package graphicinterface;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bookworm.Review;

/**
 * this class is a panel containing review title and a mascot character which
 * the user can click on it and it immediately shows a single review details.
 *
 */
public class PanelSingleReview extends JPanel implements MouseListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * some characters will appear next to a single review title. This character is chosen randomly.
	 * this linkedList only stores a String of address for images of mascots that can be used in this window. 
	 */
	private LinkedList<String> addressIconsMascotCharacters;
	private Review singleReviewAssociated;
	
	public PanelSingleReview(Review singleReviewAssociated)
	{
		this.singleReviewAssociated = singleReviewAssociated;
		//let's get the address of icons for mascot characters
		this.addressIconsMascotCharacters = new LinkedList<String>();
		this.addressIconsMascotCharacters.add("images/chibicrying.png");
		this.addressIconsMascotCharacters.add("images/chibihappy.png");
		this.createPanelSingleReview();
	}
	
	public void createPanelSingleReview()
	{
		String reviewTitle = singleReviewAssociated.getReviewTitle();
		if(reviewTitle.length() > 30)
		{
			//review title is too long: we'll have to shorten it.
			reviewTitle = reviewTitle.substring(0, 30) + "...";
		}
		JLabel labelreviewTitle = TextLabelCreator.createStyledTextlabel(reviewTitle);
		//a single review also needs a mascot character. Let's choose the character randomly.
		Random generatesrandomNumber = new Random();
		int indexMascotCharacter = generatesrandomNumber.nextInt(this.addressIconsMascotCharacters.size());
		String addressImageMascotCharcater = this.addressIconsMascotCharacters.get(indexMascotCharacter);
		JLabel labelMascotCharacter = this.getNewMascotCharacterToThisWindow(addressImageMascotCharcater);
		//now let's add the review title and mascot character in one single panel which is this class
		
		this.setBackground(new Color(255,131,137));
		GridBagLayout layoutPanelSingleReview = new GridBagLayout();
		this.setLayout(layoutPanelSingleReview);
		GridBagConstraints g = new GridBagConstraints();
		g.gridy = 0;
		g.gridheight = 1;
		this.add(labelreviewTitle, g);
		g.gridy = 1;
		g.gridheight = 3;
		this.add(labelMascotCharacter, g);
		
		//each panel of a single review can be selected via mouse. 
		this.addMouseListener(this);
	}
	
	
	
	/**
	 * given an address to a image of a mascot to be used in this window, it returns a
	 * ready JLabel with the image of the mascot.
	 * @param imageAddress address of the image where the mascot's picture is.
	 * @return a JLabel with the mascot picture.
	 */
	public JLabel getNewMascotCharacterToThisWindow(String imageAddress)
	{
		ImageIcon newMascot = CreatesImageIcon.createImageIcon(imageAddress, "mascot");
		JLabel labelForNewMascot = new JLabel(newMascot);
		return labelForNewMascot;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
		/*first, let's change the cursor to a loading cursor because constructing the single 
		 * review's screen can take a while(not too long, but it's good to give a feedback :D)
		 */
		Component objectClicked = arg0.getComponent();
		objectClicked.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		
		/*
		 * user clicked on a single review panel? then the system must open a new window
		 * for the user to read the review!
		 */
		WindowShowSingleReview windowSingleReview = new WindowShowSingleReview(this.singleReviewAssociated);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	

}
